package com.ruoyi.system.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.entity.ServiceRecord;
import com.ruoyi.system.mapper.ServiceRecordMapper;
import com.ruoyi.system.service.IServiceRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/serviceRecord")
public class ServiceRecordController extends BaseController {

    @Autowired
    private IServiceRecordService serviceRecordService;
    @Autowired
    private ServiceRecordMapper serviceRecordMapper;

    @GetMapping("/list")
    public AjaxResult list(ServiceRecord serviceRecord) {
        List<ServiceRecord> list = serviceRecordService.list(Wrappers.query(serviceRecord));
        return AjaxResult.success(list);
    }

    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable Long id) {
        ServiceRecord serviceRecord = serviceRecordService.getWithEmployees(id);
        return AjaxResult.success(serviceRecord);
    }

    @PostMapping
    public AjaxResult add(@RequestBody ServiceRecord serviceRecord, @RequestParam(required = false) List<Long> employeeIds) {
        boolean saved = serviceRecordService.saveWithEmployees(serviceRecord, employeeIds);
        return saved ? AjaxResult.success() : AjaxResult.error();
    }

    @PutMapping
    public AjaxResult edit(@RequestBody ServiceRecord serviceRecord) {
        serviceRecordService.updateById(serviceRecord);
        return AjaxResult.success();
    }

    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        //serviceRecordService.removeById(id);
        serviceRecordMapper.deleteById(id);
        return AjaxResult.success();
    }
}